package com.alibaba.product.service.impl;

import com.alibaba.basic.service.IBaiduAiAuditService;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.PetDetail;
import com.alibaba.pet.domain.PetOnlineAuditLog;
import com.alibaba.product.domain.Product;
import com.alibaba.product.domain.ProductDetail;
import com.alibaba.product.domain.ProductOnlineAuditLog;
import com.alibaba.product.mapper.ProductDetailMapper;
import com.alibaba.product.mapper.ProductMapper;
import com.alibaba.product.mapper.ProductOnlineAuditLogMapper;
import com.alibaba.product.service.IProductService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
@Autowired
private ProductDetailMapper productDetailMapper;
    @Override
    public void add(Product product) {

        super.add(product);

        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }
    @Override
    public void delete(Long id) {
        super.delete(id);
        productDetailMapper.deleteByPetId(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetailMapper.update(productDetail);

        }
    }

    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);

        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.findById(id);
            // 审核类容和图像
            Boolean aBoolean = baiduAiAuditService.textAudit(product.getName());
            Boolean aBoolean1 = baiduAiAuditService.imageAudit(product.getResources());
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);

            if (aBoolean &&  aBoolean1){
                // 该状态
                productMapper.onsale(id,new Date());
                // 添加审核日志
              log.setState(1);
                log.setNote("审核成功");
                // 没有审核人 百度智能审核
               productOnlineAuditLogMapper.add(log);

            }else {
                // 添加审核日志
                log.setState(0);
                log.setNote("审核失败");
                // 没有审核人 百度智能审核
                productOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMessage("上架失败");

            }

        }

        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        productMapper.offdale(ids,new Date());
        return new AjaxResult();
    }
}
